package com.guomzh.onlineq.controller;

import com.guomzh.onlineq.domain.EnvContext;
import com.guomzh.onlineq.domain.Question;
import com.guomzh.onlineq.domain.User;
import com.guomzh.onlineq.domain.ViewObject;
import com.guomzh.onlineq.service.CommentService;
import com.guomzh.onlineq.service.FollowService;
import com.guomzh.onlineq.service.QuestionService;
import com.guomzh.onlineq.service.UserService;
import com.guomzh.onlineq.util.OnlineQUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {
    //打印日志，配合log4j
	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
	//调用user的service层
    @Autowired
    private UserService userService;
    //调用question的service层
    @Autowired
    private QuestionService questionService;
    //调用comment(评论)的service层
    @Autowired
    private CommentService commentService;
    //调用关注的service层
    @Autowired
    private FollowService followService;
    //环境上下文
    @Autowired
    private EnvContext envContext;
    //get提交方法，从提交搜索栏中直接获取userId
    @RequestMapping(path = {"/user/{userId}"} , method = {RequestMethod.GET})
    //建立一个user的索引（id）
    //根据userid拿到user的信息
    public String userIndex(Model model, @PathVariable("userId") int userId){
    	//viewobject本身是一个map集合
        List<ViewObject> vos = getQuestions(userId,0,40);
        model.addAttribute("vos", vos);
        User user = userService.getUser(userId);//登陆的那个账户
        ViewObject vo = new ViewObject();
        vo.set("user", user);
        //获取回答的数量
        vo.set("commentCount", commentService.getUserCommentCount(userId));
        //获取粉丝数（别人关注自己的）
        vo.set("followerCount", followService.getFollowerCount(OnlineQUtil.ENTITY_USER, userId));
        //获取关注别人的数量
        vo.set("followeeCount", followService.getFolloweeCount(userId, OnlineQUtil.ENTITY_USER));
        if (envContext.getUser() != null) {
            vo.set("followed", followService.isFollower(envContext.getUser().getId(),OnlineQUtil.ENTITY_USER, userId));
        } else {
        	//
            vo.set("followed", false);
        }
        model.addAttribute("profileUser", vo);//用户的简介
        return "profile";
    }
    

    @RequestMapping(path={"/","index"}, method = {RequestMethod.GET})
    public String index(Model model){
    	//获取前40个问题，封装成视图，然后再在放到list集合中
        List<ViewObject> vos = getQuestions(0,0,40);
        //把上面的list集合放到model域中，
        model.addAttribute("vos", vos);
        return "index";
    }
    //ViewObject本来是一个map<string，Object>类型,
    //获取每一个人提问的问题，从offset到limit个
    private List<ViewObject> getQuestions(int userId, int offset,int limit){
    	//把获取的问题封装到一个list中
        List<Question> questionList=questionService.getLatestQuestion(userId,offset,limit);
        //创造一个视图的list对象
        List<ViewObject> vos =new ArrayList<>();
        for(Question question:questionList){
        	//创建一个视图对象vo
            ViewObject vo =new ViewObject();
            //利用循环，把每一个问题都封装成一个视图
            vo.set("question", question);
            //根据每一个问题的id获取每一个提出问题的用户信息，再封装到视图对象中
            vo.set("user", userService.getUser(question.getUserId()));
            //根据问题的id和。。。。。东西获取关注的数量，然后封装到视图中
            vo.set("followCount", followService.getFollowerCount(OnlineQUtil.ENTITY_QUESTION, question.getId()));
            //把封装好的这个视图，放到视图的list中
            vos.add(vo);
        }
        //返回整个封装好的视图list对象
        return vos;
    }
}
